<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
var echarts = require('echarts/lib/echarts')
// 引入 ECharts 主模块
require('echarts/lib/chart/line')
require('echarts/lib/chart/scatter')
require('echarts/lib/component/dataZoom')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/legend')
import { debounce } from '@/utils'
import { themeColor } from '@/assets/echarts/theme'
import { fullDays } from '@/utils/pickTime'
const DAY = 1000 * 60 * 60 * 24

export default {
  name: 'AutoAnalyzeWarninglineMarker',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    data: {
      type: Array,
      default: () => ([])
    },
    datazoomEnd: {
      type: Number,
      default: 35
    },
    datazoomStart: {
      type: Number,
      default: 0
    },
    tabName: {
      type: String,
      default: 'binYield'
    },
    intervalNum: {
      type: Number,
      default: 0
    },
    radioValue: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    data: {
      handler() {
        this.initChart()
      }
    },
    datazoomEnd: {
      handler() {
        this.initChart()
      }
    },
    datazoomStart: {
      handler() {
        this.initChart()
      }
    },
    intervalNum: {
      handler() {
        this.initChart()
      }
    },
    radioValue: {
      handler() {
        this.initChart()
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      var delta = Math.max.apply(Math, this.data.map(v => v.yAxis)) - Math.min.apply(Math, this.data.map(v => v.yAxis))
      var chartmin = (Math.min.apply(Math, this.data.map(v => v.yAxis)) - delta * 0.19)
      var chartmax = (Math.max.apply(Math, this.data.map(v => v.yAxis)) + delta * 0.19)
      this.chart = echarts.init(this.$el, 'themeColor')
      this.chart.setOption({
        color: themeColor,
        // ${this.tabName === 'binYield' ? '说明:' + params[0].value : ''}
        tooltip: {
          trigger: 'axis',
          formatter: (params, index) => {
            var str = ''
            for (const key in params[0].data.tips) {
              str += `${key}: ${params[0].data.tips[key]}<br />`
            }
            return `数值: ${(Number(params[0].value) * 100).toFixed(6)}%<br />
            ${str}
            违反的规则: ${params[0].data.violationRules}<br />
            `
          }
        },
        grid: {
          containLabel: false,
          bottom: 180,
          // top: 50,
          right: 0
        },
        xAxis: {
          type: 'category',
          show: true,
          // data: this.data.map(v => Number(v.xAxis)),
          data: this.intervalNum > 0 ? this.data.filter(v => Number(v.xAxis * 1000) <= (this.intervalNum * DAY) + Math.min(...this.data.map(c => Number(c.xAxis) * 1000))).map(v => {
            return {
              value: Number(v.xAxis) * 1000,
              xAxis: Number(v.xAxis) * 1000,
              yAxis: Number(v.yAxis) * 1000,
              violationRules: v.violationRules,
              tips: v.tips
            }
          })
            : this.data.map(v => {
              return {
                value: Number(v.xAxis) * 1000,
                xAxis: Number(v.xAxis) * 1000,
                yAxis: Number(v.yAxis) * 1000,
                violationRules: v.violationRules,
                tips: v.tips
              }
            }),
          axisLabel: {
            interval: 0,
            rotate: 45,
            formatter: (value) => {
              const find = this.data.map(v => {
                return {
                  value: Number(v.xAxis) * 1000,
                  xAxis: Number(v.xAxis) * 1000,
                  yAxis: Number(v.yAxis) * 1000,
                  violationRules: v.violationRules,
                  tips: v.tips
                }
              }).find(c => c.xAxis === value)
              switch (this.radioValue) {
                case 'lotId':
                  return find.tips.lotId
                case 'workOrder':
                  return find.tips.workOrder
                case 'custLotId':
                  return find.tips.custLotId
                case '':
                  return fullDays(Number(value))
                default:
                  break
              }
            }
          }
        },
        yAxis: {
          type: 'value',
          min: chartmin,
          max: chartmax,
          // data: this.data.map(v => Number(v.yAxis)),
          axisLabel: { formatter: (value, index) => {
            return this.tabName === 'binYield' ? (Number(value) * 100).toFixed(6) + '%' : value
          } }
        },
        dataZoom: [
          {
            type: 'slider',
            show: false,
            xAxisIndex: [0],
            start: this.datazoomStart,
            end: this.datazoomEnd
          }
        ],
        series: [
          {
            type: 'line',
            data: this.data.map(v => {
              return {
                value: Number(v.yAxis),
                xAxis: Number(v.xAxis) * 1000,
                yAxis: Number(v.yAxis),
                violationRules: v.violationRules,
                tips: v.tips
              }
            })
          },
          {
            symbolSize: 10,
            itemStyle: {
              color: 'red'
            },
            data: this.data.map(v => {
              return {
                value: Number(v.yAxis),
                xAxis: Number(v.xAxis) * 1000,
                yAxis: Number(v.yAxis),
                violationRules: v.violationRules,
                tips: v.tips
              }
            }),
            type: 'scatter'
          }
        ]
      })
    }
  }
}
</script>
